package com.ruoyi.asms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.ams.controller.AmsAssetTypeController;
import com.ruoyi.asms.domain.AsmsFaultAsset;
import com.ruoyi.asms.domain.vo.*;
import com.ruoyi.asms.mapper.AsmsFaultAssetMapper;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 故障资产设备信息Service业务层处理
 * 
 * @author yfkj
 * @date 2021-11-01
 */
@Service
public class AsmsFaultAssetServiceImpl extends ServiceImpl<AsmsFaultAssetMapper, AsmsFaultAsset>  implements IAsmsFaultAssetService
{
    @Autowired
    private AsmsFaultAssetMapper asmsFaultAssetMapper;

    @Autowired
    IAsmsAssetFullInfoService asmsAssetFullInfoService;

    @Autowired
    AmsAssetTypeController remoteAssetTypeService;
    /**
     * 查询故障资产设备信息
     * 
     * @param faultAssetId 故障资产设备信息主键
     * @return 故障资产设备信息
     */
    @Override
    public AsmsFaultAsset selectAsmsFaultAssetByFaultAssetId(Long faultAssetId)
    {
        return asmsFaultAssetMapper.selectAsmsFaultAssetByFaultAssetId(faultAssetId);
    }

    /**
     * 查询故障资产设备信息列表
     * 
     * @param asmsFaultAsset 故障资产设备信息
     * @return 故障资产设备信息
     */
    @Override
    public List<AsmsFaultAsset> selectAsmsFaultAssetList(AsmsFaultAsset asmsFaultAsset)
    {
        return asmsFaultAssetMapper.selectAsmsFaultAssetList(asmsFaultAsset);
    }

    /**
     * 新增故障资产设备信息
     * 
     * @param asmsFaultAsset 故障资产设备信息
     * @return 结果
     */
    @Override
    public int insertAsmsFaultAsset(AsmsFaultAsset asmsFaultAsset)
    {
        asmsFaultAsset.setCreateTime(System.currentTimeMillis());
        asmsFaultAsset.setCreateBy(SecurityUtils.getUsername());
        return asmsFaultAssetMapper.insertAsmsFaultAsset(asmsFaultAsset);
    }

    /**
     * 修改故障资产设备信息
     * 
     * @param asmsFaultAsset 故障资产设备信息
     * @return 结果
     */
    @Override
    public int updateAsmsFaultAsset(AsmsFaultAsset asmsFaultAsset)
    {
        asmsFaultAsset.setUpdateTime(System.currentTimeMillis());
        asmsFaultAsset.setUpdateBy(SecurityUtils.getUsername());

        return asmsFaultAssetMapper.updateAsmsFaultAsset(asmsFaultAsset);
    }

    /**
     * 批量删除故障资产设备信息
     * 
     * @param faultAssetIds 需要删除的故障资产设备信息主键
     * @return 结果
     */
    @Override
    public int deleteAsmsFaultAssetByFaultAssetIds(Long[] faultAssetIds)
    {
        return asmsFaultAssetMapper.deleteAsmsFaultAssetByFaultAssetIds(faultAssetIds);
    }

    /**
     * 删除故障资产设备信息信息
     * 
     * @param faultAssetId 故障资产设备信息主键
     * @return 结果
     */
    @Override
    public int deleteAsmsFaultAssetByFaultAssetId(Long faultAssetId)
    {
        return asmsFaultAssetMapper.deleteAsmsFaultAssetByFaultAssetId(faultAssetId);
    }

    @Override
    public List<FaultChartVo>  getFaultNumber(Long maintenance_dept_id, Long useDeptId, Long assetTypeId) {
        List<FaultNumberAssetTypeVo> vos= asmsFaultAssetMapper.getFaultNumberbyAssetType(maintenance_dept_id,useDeptId, assetTypeId);
        HashMap<String,Integer> map =new HashMap<>();
        for(FaultNumberAssetTypeVo vo : vos){
            if(map.containsKey(vo.getFaultReason())){
                int number = map.get(vo.getFaultReason())+1;
                map.put(vo.getFaultReason(),number);
            }else{
                map.put(vo.getFaultReason(),1);
            }
        }

        List<FaultChartVo> faultChartVos = new LinkedList<>();

        for (Map.Entry<String, Integer> entry : map.entrySet()) {

            faultChartVos.add(new FaultChartVo(entry.getKey(),entry.getValue()));

        }

        return faultChartVos;
    }

    @Override
    public AssetRepairNumberVo getRepairResultByDept(TreeSelect tree) {
        if(tree != null && tree.getKey() != null){

        }
        return null;
    }

    @Override
    public List<ThisWeekFaultSQLVo> getOneWeekFaultNum(Long startTime, Long endTime, Long mDeptId) {
        return asmsFaultAssetMapper.getOneWeekFaultNum(startTime, endTime, mDeptId);
    }

    @Override
    public AssetTypeFaultNumberVo getFaultNumberByAssetType(Long assetTypeId) {

        Long deptId = SecurityUtils.getDeptId();

        //获取下一级子类型故障信息
        List<AsmsAssetFullInfo> fullInfos = asmsAssetFullInfoService.
               list(new QueryWrapper<AsmsAssetFullInfo>().eq("type_parent_id",assetTypeId).groupBy("asset_type_id").eq("use_dept_id",deptId));


        //获取所有子类型故障信息
        List<AsmsAssetFullInfo> assetTypeFaultInfos = asmsAssetFullInfoService.
                list(new QueryWrapper<AsmsAssetFullInfo>().like("asset_type_ancestors", assetTypeId).eq("use_dept_id",deptId));

        //获取本类型故障信息
        List<AsmsAssetFullInfo> typeInfos = asmsAssetFullInfoService.
                list(new QueryWrapper<AsmsAssetFullInfo>().eq("asset_type_id",assetTypeId).eq("use_dept_id",deptId));

        AssetTypeFaultNumberVo vo =new AssetTypeFaultNumberVo();
        vo.setKey(assetTypeId);
        //如果本类型故障记录没有
        if(typeInfos == null || typeInfos.size() < 1){
            if(assetTypeId == 0l){
                vo.setTitle("0");
            }else{
                //远程获取类型名称
                vo.setTitle(remoteAssetTypeService.getInfo(assetTypeId).getData().getAssetTypeName());
            }
            vo.setNumber(0);
        }else{
            vo.setTitle(typeInfos.get(0).getAssetTypeName());
            vo.setNumber(typeInfos.size());
        }

        //如果没有下一级子类型信息
        if(fullInfos == null || fullInfos.size() <1){
            vo.setChildren(null);
            return vo;
        }else{
            List<AssetTypeFaultNumberVo> childrens = new LinkedList<>();
            for(AsmsAssetFullInfo fullInfo :fullInfos){
                childrens.add(getFaultNumberByAssetType(fullInfo.getAssetTypeId()));
            }
            vo.setChildren(childrens);
        }

        vo.setNumber(vo.getNumber()+assetTypeFaultInfos.size());

        return vo;
    }
}
